package paa.simulator.grafo.simple;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class UndirectedSimpleGraph<V extends Object> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HashMap<V, HashSet<PessoaEdge<V>>> edgeMap;

	public UndirectedSimpleGraph() {
		this.edgeMap = new HashMap<V, HashSet<PessoaEdge<V>>>();
	}
	
	public void addVertex(V v) {
		if (edgeMap.containsKey(v)) {
			throw new RuntimeException("Repeated vertex " + v);
		}
		
		edgeMap.put(v, new HashSet<PessoaEdge<V>>());
	}
	
	public void addEdge(V v1, V v2) {
		if (!edgeMap.containsKey(v1)) {
			throw new RuntimeException("Unknown vertex");
		}
		
		if (!edgeMap.containsKey(v2)) {
			throw new RuntimeException("Unknown vertex");
		}

		PessoaEdge<V> e = new PessoaEdge<V>(v1, v2);
		
		edgeMap.get(v1).add(e);
		edgeMap.get(v2).add(e);
	}

	public boolean containsVertex(V v) {
		return edgeMap.containsKey(v);
	}

	public Collection<PessoaEdge<V>> edgesOf(V v) {
		if (!edgeMap.containsKey(v)) {
			throw new RuntimeException("Unknown vertex");
		}
		
		return this.edgeMap.get(v);
	}

	public Set<V> vertexSet() {
		return edgeMap.keySet();
	}
}